<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.PreparedStatement" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="altri.database" %>
<%@page import="java.util.ArrayList" %>
<%@page import="altri.Articoli" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="CSSHome.css" media="all">
        <title>Index html</title>
    </head>
    <body>
        <%
                    HttpSession hs = request.getSession(true);
                    Connection conn = database.connessione();
        %>
        <img id="top" src="img/top.png" alt="">
        <div id="logo">
        <div id="menu">
            <ul>
                <li><a href="index.jsp" >Home</a>
            </ul>
        </div>
        </div>
        <div id="contenitore">
            <div id="contenitore_articoli">

                <h2>Ultimi articoli</h2>
                <%
                            int artPerPag = 6;
                            int inf = 0;
                            int sup = artPerPag - 1;
                            int infArt = 0;
                            int supArt = artPerPag - 1;

                            if (request.getParameter("inf") != null && request.getParameter("sup") != null) {
                                infArt = Integer.valueOf(request.getParameter("inf"));
                                supArt = Integer.valueOf(request.getParameter("sup"));
                            }

                            PreparedStatement ps1 = conn.prepareStatement("SELECT * FROM Articoli", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                            ResultSet rs1 = ps1.executeQuery();

                            ArrayList articoli = new ArrayList();

                            while (rs1.next()) {
                                Articoli art = new Articoli();
                                art.setId(rs1.getInt("Id"));
                                art.setTitolo(rs1.getString("Titolo"));
                                art.setTesto(rs1.getString("Testo"));
                                art.setAutore(rs1.getString("Nick"));
                                art.setCategoria(rs1.getInt("Categoria"));
                                articoli.add(art);
                            }
                            int el = infArt;
                            while (el < articoli.size() && el <= supArt) {

                %>
                <div class="articolo">
                    <% int id=((Articoli) articoli.get(el)).getId();
                    int categoria=((Articoli) articoli.get(el)).getCategoria();%>


                    <bean:define id="id" name="Id" property=<%=Integer.toString(id) %> />
                    <bean:define id="cat" name="Cat" property=<%=Integer.toString(categoria) %> />
                  <%  java.util.HashMap params = new java.util.HashMap();
                      params.put("id", Integer.toString(id));
                      params.put("categoria", Integer.toString(categoria));
                      pageContext.setAttribute("parameters", params);  
                  %>
                  <h3><html:link action="/articolisimili.do" name="parameters" scope="page" > <%=((Articoli) articoli.get(el)).getTitolo() %> </html:link></h3>
                    
                    <%                             
                                                    out.println("<div>" + ((Articoli) articoli.get(el)).getTesto() + "</div>");
                                                    out.println("<h4>Autore: " + ((Articoli) articoli.get(el)).getAutore() + "</h4>");

                                                    PreparedStatement ps2 = conn.prepareStatement("SELECT * FROM Categorie WHERE id = ?",
                                                            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                                                    ps2.setInt(1, ((Articoli) articoli.get(el)).getCategoria());
                                                    ResultSet rs2 = ps2.executeQuery();
                                                    while (rs2.next()) {
                                                        out.println("<h4>Categoria: " + rs2.getString("Nome") + "</h4>");
                                                    }
                                                    ps2.close();

                                                    PreparedStatement psImg = conn.prepareStatement("SELECT * FROM Immagini WHERE idArticolo = ?",
                                                            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                                                    psImg.setInt(1, ((Articoli) articoli.get(el)).getId());
                                                    ResultSet rsImg = psImg.executeQuery();

                                                    while (rsImg.next()){
                                                        %>
                                                        <img src="<%=rsImg.getString("Path")%>" width="175" height="175" />
                                                        <%
                                                    }

                                                    PreparedStatement psVid = conn.prepareStatement("SELECT Path,Formato FROM Video WHERE idArticolo = ?",
                                                            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                                                    psVid.setInt(1, ((Articoli) articoli.get(el)).getId());
                                                    ResultSet rsVid = psVid.executeQuery();

                                                    while (rsVid.next()){
                                                        if (rsVid.getString("Formato").equals("youtube")){
                                                         String s=rsVid.getString("Path").substring(31,rsVid.getString("Path").length());
                                                    %>
                                                       <object type='application/x-shockwave-flash' data='http://www.youtube.com/v/<%=s%>' width='425' height='350'><param name='movie' value='http://www.youtube.com/v/<%=s%>' /></object>
                                                    <%}
                                                      else if (rsVid.getString("Formato").equals("google")){
                                                          String s=rsVid.getString("Path").substring(40,rsVid.getString("Path").length());
                                                          %>
                                                          <object type='application/x-shockwave-flash' data='http://video.google.com/googleplayer.swf?docId=<%=s%>' width='425' height='350'><param name='movie' value='http://video.google.com/googleplayer.swf?docId=<%=s%>' /></object>
                                                          <%
                                                      }
                                                    }
                                                    el++;

                    %>
                </div>
                <%
                            }
                            PreparedStatement ps3 = conn.prepareStatement("SELECT COUNT(*) AS NArt From Articoli");
                            ResultSet numArt = ps3.executeQuery();
                            numArt.next();
                            int nart = numArt.getInt("NArt");
                            int numeroPagine=1;
                            if ((int)(nart % artPerPag)==0){
                             numeroPagine = (int) (nart / artPerPag) ;
                            }else{
                               numeroPagine = (int) (nart / artPerPag)+1 ;
                            }
                            int i = 1;
                            out.print("Pagine: ");
                            while (i <= numeroPagine) {
                %>
                <a href="index.jsp?inf=<%=inf%>&sup=<%=sup%>" ><%=i%></a>
                <%
                                inf = inf + artPerPag;
                                sup = sup + artPerPag;

                                i++;
                            }
                            ps3.close();
                            ps1.close();

                %>


            </div>
            <div id="col_destra">
                <%
                            PreparedStatement ps = conn.prepareStatement("SELECT id,Nome,Categoria FROM Categorie ORDER BY id, Categoria ",
                                    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                            ResultSet rs = ps.executeQuery();
                %>
                <div id="ricerca">
                    <h2>Ricerca</h2>
                    <form action="ricercaArticoli.do" method="POST">
                        <div id="box_cat">
                            <select name="categorie" Id="categorie">
                                <%
                                            while (rs.next()) {
                                %>
                                <option value="<%=rs.getString("id")%>"><%=rs.getString("Nome")%></option>
                                <%
                                            }
                                %>
                            </select><br/>
                            <%
                                PreparedStatement psAttr = conn.prepareStatement("SELECT * FROM Attributi",
                                    ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                            ResultSet rsAttr = psAttr.executeQuery();

                              while (rsAttr.next()){
                                
                                 String nome = rsAttr.getString("Nome");
                                 String tipo = rsAttr.getString("Tipo");

                                        if (tipo.equalsIgnoreCase("Booleano")) {%>
                    <%=nome%>:<input type="checkbox" name="<%=nome %>" /><br>
                    <%
                                   }
                                   if (tipo.equalsIgnoreCase("Intero")) {
                    %>
                  <label><%=nome%>:</label><input type="text" name="<%=nome%>" value=""/>  <br>
                    <% }
                                   if (tipo.equalsIgnoreCase("Stringa")) {
                    %>
                    <label><%=nome%>:</label><input type="text" name="<%=nome%>" value=""/> <br>
                    <% }
                                   if (tipo.equalsIgnoreCase("Time")) {
                    %>
                    <label><%=nome%>:</label>
                    <input type="hidden" name="<%=nome %>" value="<%=nome %>" />
                    Giorno: <select name="<%=nome%>Giorno">
                        <option value="">---</option>
                        <% for (int j=1;j<32;j++){ %>
                        <option value="<%=j %>"><%=j %> </option>
                        <%  } %>
                    </select>
                    Mese: <select name="<%=nome%>Mese">
                        <option value="">---</option>
                        <% for (int j=1;j<13;j++){ %>
                        <option value="<%=j %>"><%=j %> </option>
                        <%  } %>
                    </select>
                    Anno: <select name="<%=nome%>Anno">
                        <option value="">---</option>
                        <% for (int j=2000;j<2021;j++){ %>
                        <option value="<%=j %>"><%=j %> </option>
                        <%  } %>
                    </select>
                    <br>
                    <% }

                                
                              }
                            %>
                        </div>
                        <div id="submit">
                            <input type="submit" value="Cerca" />
                        </div>
                    </form>
                </div>
                <%
                            ps.close();
                            conn.close();
                %>

                <%
                            Object login = hs.getAttribute("login");
                            if (!(login != null && (Boolean) login)) {
                %>
                <div id="login">
                    <h2>Esegui il Login</h2>
                    <form id="form_login" action="Login.do" method="POST">
                        <ul>
                            <li>
                                <label class="description"  for="nickname">Username: </label>
                                <div>
                                    <input id="nickname" name="Nickname" class="box_form" type="text" maxlength="255" value=""/>
                                </div>
                            </li>
                            <li>
                                <label class="description" for="password">Password: </label>
                                <div>
                                    <input id="password" name="Password" class="box_form" type="password" maxlength="255" value=""/>
                                </div>
                            </li>
                            <li class="buttons">
                                <input id="saveForm" class="button_text" type="submit" name="invia" value="Invia" />
                            </li>
                        </ul>
                    </form>
                    <div class="destra"> Se NON sei un utente registrato esegui la <a href="registrazione.jsp"><b>REGISTRAZIONE</b></a>
                    </div>
                </div>
                <% }%>
                <%
                            if ((login != null && (Boolean) login)) {
                %>
                <div id="dati_personali">
                    <h2>Benvenuto "<%= ((String) session.getAttribute("Nickname")).toString()%>"!</h2>
                    <div class="link_centrale"><a href="logout.do">Logout</a></div>
                </div>
                <div id="menuDestro">
                    <ul>
                        <li><a href="ModificaUserArt.jsp">Visualizza e modifica i tuoi articoli</a></li>
                        <li><a href="nuovo_articolo.jsp">Inserisci articolo</a></li>
                        <li><a href="DeleteUserArt.jsp">Elimina articolo</a></li>
                        <% if ((Boolean)hs.getAttribute("isAdmin")){ %>
                        <li><a href="RiepilogoAttributi.jsp">Gestione attributi</a></li>
                        <li><a href="InsertAttributi.jsp">Inserimento attributi</a></li>
                        <li><a href="InsertCategory.jsp">Inserisci categoria</a></li>
                        <li><a href="RiepilogoCategorie.jsp">Riepilogo categorie</a></li>
                        <%}%>
                    </ul>
                </div>
                <% }%>
            </div>
        </div>
        <img id="bottom" src="img/bottom.png" alt="">
    </body>
</html>

